package com.whz.lambdademo;

import java.util.function.DoubleBinaryOperator;

/**
 * @Author: sinosoft
 * @Description: 枚举实例域优先于特定于常量的类主体
 * @Date: Create in 18:37 2021/5/17
 * @Modified By:
 */
public enum Operation {
    PLUS("+", (x, y) -> x + y),
    MINUS("-", (x, y) -> x - y),
    TIMES("*", (x, y) -> x * y),
    DIVIDE("/", (x, y) -> x / y)
    ;
    private final String symbol;
    // java.util.function 中的预定义函数接口
    // 表示带有两个double参数的函数 并返回一个double结果
    private final DoubleBinaryOperator op;

    Operation(String symbol, DoubleBinaryOperator op) {
        this.symbol = symbol;
        this.op = op;
    }

    @Override
    public String toString() {
        return symbol;
    }

    public double apply(double x, double y) {
        return op.applyAsDouble(x, y);
    }
}
